<!--
 * @Descripttion: 系统计划任务配置
 * @version: 1.2
 * @Author: sakuya
 * @Date: 2021年7月7日09:28:32
 * @LastEditors: sakuya
 * @LastEditTime: 2021年7月10日20:56:47
-->

<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="700" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="任务标题" prop="jobDesc">
				<el-input v-model="form.jobDesc" placeholder="计划任务标题" clearable></el-input>
			</el-form-item>
			<el-form-item label="执行器类" prop="executorHandler">
				<el-input v-model="form.executorHandler" placeholder="计划任务执行类名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="定时规则" prop="scheduleConf">
				<sc-cron v-model="form.scheduleConf" placeholder="请输入Cron定时规则" clearable :shortcuts="shortcuts"></sc-cron>
			</el-form-item>
			<el-form-item label="是否启用" prop="triggerStatus">
				<el-switch v-model="form.triggerStatus" :active-value="1" :inactive-value="0"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>

		<el-row>
			<el-col>
				<b>下次调度时间:</b>
			</el-col>
			<dev  v-if="form.scheduleConf">
				<el-col v-for="cron in cronList" v-bind:key="cron">
					{{cron}}
				</el-col>
			</dev>
		</el-row>

	</el-dialog>
</template>

<script>
	import scCron from '@/components/scCron';

	export default {
		components: {
			scCron
		},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增计划任务',
					edit: '编辑计划任务'
				},
				form: {
					id:"",
					jobDesc: "",
					scheduleConf: "",
					triggerStatus: "0"
				},
				rules: {
					jobDesc:[
						{required: true, message: '请填写标题'}
					],
					executorHandler:[
						{required: true, message: '请填写执行类'}
					],
					scheduleConf:[
						{required: true, message: '请填写定时规则'}
					],
					triggerStatus:[
						{required: true, message: '请选择是否启用'}
					]
				},
				visible: false,
				isSaveing: false,
				shortcuts: [
					{
						text: "每天8点和12点 (自定义追加)",
						value: "0 0 8,12 * * ?"
					}
				],
				cronList:[]
			}
		},
		watch:{
			'form.scheduleConf'(newVal,oldVal){
				if(newVal != oldVal && newVal != ""){
					this.cronChange(newVal)
				}
			}
		},

		mounted() {

		},
		methods: {

			async cronChange(data){
				if(!data){
					return ;
				}
				let res = await this.$API.system.task.nextTriggerTime.get(this.form);
				if (res.status != 200) {
					return;
				}
				this.cronList = res.data
			},

			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法
			async submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						let res = {}
						if (this.mode == 'add') {
							res = await this.$API.system.task.add.post(this.form);
							this.$emit('success')
							this.visible = false;
						} else {
							res = await this.$API.system.task.edit.put(this.form);
							this.$emit('success')
							this.visible = false;
						}
						if (res.status != 200) {
							return;
						}
						this.$message.success("操作成功")
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.jobDesc = data.jobDesc
				this.form.executorHandler = data.executorHandler
				this.form.scheduleConf = data.scheduleConf
				this.form.triggerStatus = data.triggerStatus
			}
		}
	}
</script>

<style>
</style>
